import './public-path'
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import clearKeepAliveCache from '@/utils/clearKeepAliveCache'
import VueECharts from 'vue-echarts'
import Print from 'vue-print-nb'
Vue.use(Print) //注册
Vue.component('VCharts', VueECharts)
Vue.config.productionTip = false
Vue.prototype.isMicro = window.__POWERED_BY_QIANKUN__

//动态加载js脚本
// import '@/utils/loadScript'
// 安装插件
import '@/plugins'

// 注册全局组件
import '@/components/gobalComponents.js'

// 系统初始化样式
import '@/scss/reset.css'
import '@/scss/input.css'

// 自定义指令
import '@/directives'

// 引入icon
import '@/icons/iconfont.css'

// 注册国际化
import i18n from '@/lang/index'

let vm = null
function render(props = {}) {
  const { container } = props
  vm = new Vue({
    router,
    store,
    i18n,
    render: (h) => h(App)
  }).$mount(container ? container.querySelector('#app') : '#app')
  Vue.prototype.closeTab = props.closeTab || null // 增加全局的关闭选项卡功能，如果子应用独立运行，就是null
}

export let cachedInstanceMap = {} // keep-alive缓存列表

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render()
}

export async function bootstrap() {
  console.log('[vue] vue app bootstraped')
}

export async function mount(props) {
  props.onGlobalStateChange((state, prev) => {
    // 处理缓存
    if (state['mes'] && Array.isArray(state['mes']) && state['mes'].length > 0) {
      clearKeepAliveCache(state['mes'])
    } else {
      cachedInstanceMap = {}
    }
    console.log(111, store)
    // 处理刷新页面
    state['refreshKey'] && store.commit('SET_REFRESH_KEY', state['refreshKey'])
  })
  render(props)
}

export async function unmount() {
  vm.$destroy()
  vm.$el.innerHTML = ''
  vm = null
}
